import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.Enumeration;

@WebServlet("/http")
public class Http extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Enumeration<String> headerNames = req.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String headerName = headerNames.nextElement();
            String headerValue = req.getHeader(headerName);
            System.out.println(headerName + " : " + headerValue);
        }
        String agent = req.getHeader("user-agent");
        if (agent.contains("Chrome")) {
            System.out.println("Chrome");
        }
        String referrer = req.getHeader("referer");
        System.out.println("Referer: " + referrer);
        if (referrer != null) {
            if (referrer.contains("http://localhost:8080/biji/login.html")){
                System.out.println("合法访问");
            }else {
                System.out.println("非法访问");
            }
        } else {
            System.out.println("referrer为空");
        }


    }


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        System.out.println("post");
        System.out.println(username);
    }
}
